﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model.Common.Infrastructure;
using CCF20Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using CCF20Model.ModelBase;
using System.Windows;
using CCF20Module.DeviceState;
using WiseLink.RDS.Resource;

namespace CCF20Module
{
    class InstockViewModel : ObservableObject
    {
        DeviceHandler currentDeviceHandler;
        public InstockViewModel()
        {
            CreateModel();
        }

        
        private DeviceModel deviceModel;
        private CustomerModel customerModel;
        private VehicleModel vehicleModel;
        
        private void CreateModel()
        {
            ModelFactory modelFactory = new ModelFactory();
            deviceModel = (DeviceModel)modelFactory.CreateModel(ModelName.DeviceModel);
            customerModel = (CustomerModel)modelFactory.CreateModel(ModelName.CustomerModel);
            vehicleModel = (VehicleModel)modelFactory.CreateModel(ModelName.VehicleModel);
            DeviceGridSource = deviceModel.GetDevice();
            CustomerSource = customerModel.GetSimpleCustomerInfo();
        }


        #region Properties

        private string _sn;
        public string SN
        {
            get
            {
                return _sn;
            }
            set
            {
                this.RemoveError("SN");

                if (string.IsNullOrEmpty(value))
                {
                    this.AddError("SN", "SN 卡号不可为空");
                }
                else if (customerModel.IsDuplicateSN(value, _currentDevice.DeviceID))
                {
                     this.AddError("SN", "SN号不可重复");
                }
                _sn = value;
                RaisePropertyChanged("SN");
            }
        }

        private string _snKey;
        public string SNKey
        {
            get
            {
                return _snKey;
            }
            set
            {
                _snKey = value;
                RaisePropertyChanged("SNKey");
            }
        }

        private DropDownItem _currentIDS;
        public DropDownItem CurrentIDS
        {
            get
            {
                return _currentIDS;
            }
            set
            {
                
                _currentIDS = value;
                
            }
        }


        private Device _currentDevice;
        public Device CurrentDevice
        {
            get
            {

                return _currentDevice;
            }
            set
            {
                _currentDevice = value;
                currentDeviceHandler = new DeviceHandler(deviceModel,vehicleModel, _currentDevice, _deviceGridSource);
                RaisePropertyChanged("CurrentDevice");
            }
        }

        private SimpleCustomerInfo _currentCustomer;
        public SimpleCustomerInfo CurrentCustomer
        {
            get
            {

                return _currentCustomer;
            }
            set
            {
                _currentCustomer = value;

                RaisePropertyChanged("CurrentCustomer");
            }
        }

        private ObservableCollection<SimpleCustomerInfo> _customerSource;
        public ObservableCollection<SimpleCustomerInfo> CustomerSource
        {
            get
            {

                return _customerSource;
            }
            set
            {
                _customerSource = value;
                RaisePropertyChanged("CustomerSource");
            }
        }
        private ObservableCollection<Device> _deviceGridSource;
        public ObservableCollection<Device> DeviceGridSource
        {
            get
            {

                return _deviceGridSource;
            }
            set
            {
                _deviceGridSource = value;
                RaisePropertyChanged("DeviceGridSource");
            }
        }

        #endregion 
        #region command

        

        public ICommand CmdRegisterDevice
        {
            get { return new RelayCommand(RegisterDevice, CanRegisterDevice); }
        }

        private void RegisterDevice()
        {

            if (_currentDevice.IsRegistered == true)
            {
                currentDeviceHandler.UnRegisterIDC();
            }
            else
            {
                currentDeviceHandler.RegisterIDC(_sn, _snKey,_currentIDS.name,_currentIDS.value.ToString());
                                
            }
            
        }
        private bool CanRegisterDevice()
        {
            bool isSoldNotRegistered = _currentDevice != null && _currentDevice.DeviceStatus == DeviceStatus.Sold && _currentDevice.IsRegistered != true;
            bool isSoldRegistered = _currentDevice != null && _currentDevice.DeviceStatus == DeviceStatus.Sold && _currentDevice.IsRegistered == true;
            if (isSoldRegistered || (isSoldNotRegistered && !string.IsNullOrEmpty(_sn) && !string.IsNullOrEmpty(_snKey) && string.IsNullOrEmpty(this.Error)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public ICommand CmdInstockDevice
        {
            get { return new RelayCommand(InstockDevice, CanInstockDevice); }
        }

        private void InstockDevice()
        {
            currentDeviceHandler.InStock();
            //To Do: Clean up the SMS, Contact History

            CustomerSource = customerModel.GetSimpleCustomerInfo();
        }
        private bool CanInstockDevice()
        {
            if (_currentDevice != null && _currentDevice.DeviceStatus != DeviceStatus.InStock &&_currentDevice.IsRegistered == false)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public ICommand CmdSellDevice
        {
            get { return new RelayCommand(SellDevice, CanSellDevice); }
        }

        private void SellDevice()
        {
            currentDeviceHandler.SellToCustomer(_currentCustomer.ID);
            CustomerSource = customerModel.GetSimpleCustomerInfo();
        }
        private bool CanSellDevice()
        {
            if (_currentCustomer !=null && _currentDevice != null && _currentDevice.IsRegistered != true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public ICommand CmdRefresh
        {
            get { return new RelayCommand(Refresh); }
        }

        private void Refresh()
        {
            CreateModel();
        }
        public ICommand CmdSpecialIDC
        {
            get { return new RelayCommand(SpecialIDC, CanSpecialIDC); }
        }

        private void SpecialIDC()
        {
            if (_currentDevice.IsSpecialIDC == true)
            {
                currentDeviceHandler.UnRegisterSpecialIDC();
            }
            else
            {
                currentDeviceHandler.RegisterSpecialIDC();
            }
            
        }
        private bool CanSpecialIDC()
        {
            if (_currentDevice != null && _currentDevice.IsRegistered == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public ICommand CmdDeleteDevice
        {
            get { return new RelayCommand(DeleteDevice, CanDeleteDevice); }
        }

        private void DeleteDevice()
        {
            _deviceGridSource.Remove(_currentDevice);
            deviceModel.Update();
            RaisePropertyChanged("DeviceGridSource");

        }
        private bool CanDeleteDevice()
        {
            if (_currentDevice != null && _currentDevice.DeviceStatus == DeviceStatus.InStock)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
#endregion
    }
}
